﻿using System;
using System.Windows.Input;

namespace Atha.Clients.ViewModels
{
	public class DelegateCommand : ICommand
	{
		private bool _canExecute;

		private readonly Action _execute;

		public DelegateCommand(
			bool canExecute,
			Action execute)
		{
			this._canExecute = canExecute;
			this._execute = execute;
		}

		public event EventHandler CanExecuteChanged;

		public bool CanExecute(object parameter)
		{
			return this._canExecute;
		}

		public void Execute(object parameter)
		{
			this._execute();
		}

		public bool IsExecutable
		{
			get { return this._canExecute; }

			set
			{
				if (value != this._canExecute)
				{
					this._canExecute = value;

					if (this.CanExecuteChanged != null)
						this.CanExecuteChanged(this, EventArgs.Empty);
				}
			}
		}
	}
}